package com.example.administrator.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.bean.AlbumFolder;
import com.example.administrator.bean.AlbumImage;
import com.example.administrator.impl.OnCompatItemClickListener;
import com.example.administrator.mypictureshow.R;
import com.example.administrator.utils.DisplayUtils;
import com.example.administrator.utils.ImageLocalLoader;

import java.util.List;

/**
 * 相册列表适配器
 * Created by Administrator on 2018\8\1 0001.
 */

public class DialogFolderAdapter extends RecyclerView.Adapter<DialogFolderAdapter.FolderViewHolder>{

    private List<AlbumFolder> mAlbumFolders;
    private OnCompatItemClickListener mItemClickListener;
    private int checkPosition = 0;
    private static int size = DisplayUtils.dip2px(100);

    public DialogFolderAdapter(List<AlbumFolder> mAlbumFolders,OnCompatItemClickListener mItemClickListener) {
        this.mAlbumFolders = mAlbumFolders;
        this.mItemClickListener = mItemClickListener;
    }

    @Override
    public DialogFolderAdapter.FolderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FolderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.album_item_album_dialog_folder, parent, false));
    }

    @Override
    public void onBindViewHolder(DialogFolderAdapter.FolderViewHolder holder, final int position) {
        final int newPosition = holder.getAdapterPosition();
        holder.setData(mAlbumFolders.get(newPosition));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlbumFolder albumFolder = mAlbumFolders.get(newPosition);
                if (mItemClickListener != null){
                    mItemClickListener.onItemClick(v, newPosition);
                }
                if (!albumFolder.isChecked()) {
                    albumFolder.setChecked(true);
                    mAlbumFolders.get(checkPosition).setChecked(false);
                    notifyItemChanged(checkPosition);
                    notifyItemChanged(newPosition);
                    checkPosition = newPosition;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mAlbumFolders == null ? 0 : mAlbumFolders.size();
    }

    public class FolderViewHolder extends RecyclerView.ViewHolder {

        ImageView mIvImage;
        TextView mTvTitle;

        public FolderViewHolder(View itemView) {
            super(itemView);
            mIvImage = (ImageView) itemView.findViewById(R.id.iv_gallery_preview_image);
            mTvTitle = (TextView) itemView.findViewById(R.id.tv_gallery_preview_title);
        }
        public void setData(AlbumFolder albumFolder) {
            List<AlbumImage> albumImages = albumFolder.getPhotos();
            mTvTitle.setText("(" + albumImages.size() + ") " + albumFolder.getName());

            if (albumImages.size() > 0) {
                ImageLocalLoader.getInstance().loadImage(mIvImage, albumImages.get(0).getPath(), size, size);
            } else {
                mIvImage.setImageDrawable(ImageLocalLoader.DEFAULT_DRAWABLE);
            }
        }
    }
}
